package com.arpa.oms.controller;

import com.arpa.core.utils.collection.CollectionUtil;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.oms.domain.entity.OmsCustomer;
import com.arpa.oms.domain.enums.OmsGoodTypeEnum;
import com.arpa.oms.service.IOmsGoodsRelationService;
import com.arpa.oms.service.IOmsGoodsService;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.entity.Goods;
import com.arpa.wms.domain.vo.WarehouseVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;
import com.arpa.wms.domain.entity.Goods;
import com.arpa.wms.domain.enums.StatusEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: Allen chen
 * @Date 2020/8/12
 * @Description: 商品管理 controller
 */
@Api(tags = "商品")
@RestController
@RequestMapping(value = "/omsGoods", produces = "application/json;charset=UTF-8")
@Log4j2
public class OmsGoodsController {

    private final IOmsGoodsService omsGoodsService;

    private final IOmsGoodsRelationService omsGoodsRelationService;


    public OmsGoodsController(IOmsGoodsService omsGoodsService, IOmsGoodsRelationService omsGoodsRelationService) {
        this.omsGoodsService = omsGoodsService;
        this.omsGoodsRelationService = omsGoodsRelationService;
    }

    /**
     * XYTODO TODO “商品模糊查询”调整版，查询的结果并不拼装商品其他信息，如基本单位名称、批次规则等，在商品使用时再使用makeGoods方法拼装；
     * 拟使用本方法替换search方法，但由于前端使用search的地方过多，暂没仔细分析具体应用场景，所以先增加本方法，调整部分使用比较迫切的方法。
     *
     * @param keywords
     * @param shipmentCode
     * @param request
     * @return
     */
    @ApiOperation(value = "模糊查询入驻商商品，只查询缓存，不拼装商品其他信息", notes = "模糊查询入驻商商品，只查询缓存，不拼装商品其他信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "keywords", value = "商品名称、编码、条码", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "shipmentCode", value = "入驻商code", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "hasCompose", value = "是否查询组合商品, 默认查询", dataType = "boolean")
    })
    @NeedLogin
    @GetMapping(value = "/searchPure")
    public Result searchPure(@RequestParam(value = "keywords", defaultValue = "") String keywords,
                             @RequestParam(value = "shipmentCode", required = true) String shipmentCode,
                             @RequestParam(value = "hasCompose", defaultValue = "false") boolean hasCompose,
                             HttpServletRequest request) {
        try {
            List<Map<String, Object>> result = omsGoodsService.searchPure(keywords, shipmentCode);
            if (hasCompose) {
                // 组合商品
                List<Map<String, Object>> goodsRelationList = omsGoodsRelationService.searchPure(keywords, shipmentCode);
                if (null == result) {
                    result = new ArrayList<>();
                }
                if (CollectionUtil.isNotEmpty(goodsRelationList)) {
                    result.addAll(goodsRelationList);
                }
            }
            return Result.ok().data(result);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation(value = "根据商品code，组装商品各信息数据", notes = "根据商品code，组装商品各信息数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "商品code", paramType = "path", dataType = "String")
    })
    @NeedLogin
    @GetMapping(value = "/makeGoods")
    public Result makeGoods(@RequestParam(value = "code",defaultValue = "") String code, @RequestParam(value = "recordType",defaultValue = "0") Integer recordType, HttpServletRequest request) {
        try {
            Map<String, Object> result = omsGoodsService.makeGoods(code, recordType);
            return Result.ok().data(result);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取 客户下拉列表数据
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "Oms获取商品下拉列表数据", notes = "获取 商品下拉列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = WarehouseVO.class))
    @NeedLogin
    @GetMapping("/chooseData")
    public Result chooseData(HttpServletRequest request) {
        List<Goods> omsCustomers = omsGoodsService.list(new LambdaQueryWrapper<Goods>()
                .select(Goods::getCode, Goods::getName)
                .eq(Goods::getShipmentCode, UserUtil.getLoginWarehouseCodeOrShipmentCode())
                .eq(Goods::getStatus, NumConst.NUM_ZERO)
        );
        return Result.ok().data(omsCustomers);

    }


    @ApiOperation(value = "返回所有入驻商商品", notes = "返回所有入驻商商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @GetMapping(value = "/selectGoods")
    public Result selectGoods(HttpServletRequest request) {
        try {
            List<Map<String, Object>> list = omsGoodsService.listMaps(new QueryWrapper<Goods>().lambda().select(Goods::getCode, Goods::getName, Goods::getGoodCode, Goods::getBarCode).eq(Goods::getShipmentCode, UserUtil.getLoginWarehouseCodeOrShipmentCode()).eq(Goods::getRecordType, OmsGoodTypeEnum.GOOD_TYPE_REAL.getCode()).eq(Goods::getStatus, StatusEnum.ACTIVE.getValue()).orderByAsc(Goods::getName));
            return Result.ok().data(list);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 拟使用本方法替换search方法，但由于前端使用search的地方过多，暂没仔细分析具体应用场景，所以先增加本方法，调整部分使用比较迫切的方法。
     *
     * @param goodName
     * @param goodCode
     * @param goodBarCode
     * @param shipmentCode
     * @param request
     * @return
     */
    @ApiOperation(value = "模糊查询入驻商商品，只查询缓存，不拼装商品其他信息", notes = "模糊查询入驻商商品，只查询缓存，不拼装商品其他信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "goodName", value = "商品名称", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "goodCode", value = "商品编码", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "goodBarCode", value = "商品条码", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "shipmentCode", value = "入驻商code", paramType = "path", dataType = "String"),
            @ApiImplicitParam(name = "hasCompose", value = "是否查询组合商品, 默认查询", dataType = "boolean")
    })
    @NeedLogin
    @GetMapping(value = "/searchGoods")
    public Result searchGoods(@RequestParam(value = "goodName", defaultValue = "") String goodName,
                             @RequestParam(value = "goodCode", defaultValue = "") String goodCode,
                             @RequestParam(value = "goodBarCode", defaultValue = "") String goodBarCode,
                             @RequestParam(value = "shipmentCode", defaultValue = "") String shipmentCode,
                             @RequestParam(value = "hasCompose", defaultValue = "true") boolean hasCompose,
                             HttpServletRequest request) {
        try {
            List<Map<String, Object>> result = omsGoodsService.searchGoods(goodName,goodCode,goodBarCode,shipmentCode);
            if (hasCompose) {
                // 组合商品
                List<Map<String, Object>> goodsRelationList = omsGoodsRelationService.searchGoods(goodName,goodCode,goodBarCode,shipmentCode);
                if (null == result) {
                    result = new ArrayList<>();
                }
                if (CollectionUtil.isNotEmpty(goodsRelationList)) {
                    result.addAll(goodsRelationList);
                }
            }
            return Result.ok().data(result);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

}
